/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FamousBallet;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.print.PageFormat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 *<pre>
 File name  Form.java
 * @author    <b><i>Yerong</i></b>
 * Instructor  Niko Culevski
 * Assignment  Project_3
 * @version   1.0.1
 * Platform    PC, Windows 10, NetBeans 8.1
 * @see       javax.swing.JFrame
 * @see       java.awt.Toolkit
 * @see       java.io.File
 * @see       java.io.FileNotFoundException
 * @see       java.awt.Graphics
 * @see       java.io.IOException
 * @see       java.net.URI
 * @see       java.util.ArrayList
 * @see       java.util.Scanner
 * @see       java.util.logging.Level
 * @see       java.util.logging.Logger
 * @see       javax.swing.JOptionPane
 *******************************************************************************
* This program is a ballet database management tool. You can read the ballet da-
* -ta base file start with the item number and all the information seperated by 
* "~~". You can check, add, edit, and delete the ballet data with its name, compo-
* -ser, city first performed, year first performed, and synopsis and link to yo-
* -utube. On the left is a list with all the ballets, and on the right are all
* the information about the ballet. You can click on the synopsis to view the f-
* ull text and click on the link to access the youtube vide. On the bottom, the-
* -re are buttons that operate the data. Add button can add a new ballet data, 
* but you can't creat a ballet has the same information as the one on the datab-
* -ase. You can edit a ballet by clicking on the edit button, and also, you can
* not edit the one with the same information as the one on the database. Delete
* button will delete the ballet data, but before deleting, there is a confirmat-
* -ion box. Print button will print the ballet you select. Exit button will exit 
* the program.
* On the file menu, you can print the whole gui, search a ballet, or exit.
* On the sort menu, you can sort ballets by their name or by year.
* On the management menu, you can add, delete, or edit the ballet.
* On the help menu, you can check the about form.
 *******************************************************************************
 * Input:    "famous" database file
 * Output:   The information about some famous ballets
 *******************************************************************************
 * </pre>
 */
public class Form extends javax.swing.JFrame {

    /**
     * Creates new form Form
     */
    ArrayList<Ballet> famous = new ArrayList();
    private final String FILE_NAME = "src/FamousBallet/famous";
    
    
    public Form() {
        initComponents();
        //Set the form in the center of the screen
        this.setLocationRelativeTo(null);
        //Set the Calculate button as default. It fires when you press "Enter."
        this.getRootPane().setDefaultButton(addJButton);
        //Set icon
        this.setIconImage(
                Toolkit.getDefaultToolkit().getImage("src/FamousBallet/Bunny.png"));
        readFromFile(FILE_NAME);
        
        //clear the JList
        famousJList.removeAll();
        
        //Display all the Ballets in the file.
        displayBallets();
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method reWriteFile()
     * Rewrite the database file and reload it after modifing the data.
     * @see java.util.ArrayList
     * @author Yerong
     * Date: 11/29/2016
     */        
    public void reWriteFile(){
        StringBuilder output = new StringBuilder();
        
        //Append the number of ballets first
        output.append(String.valueOf(famous.size()));
        
        //Iterate the arraylist and append the data text.
        for(int i=0;i<famous.size();i++){
            output.append(famous.get(i).toData());
        }
        
        System.out.println(output.toString());
        
        //Try to save the file
        try {
            //Save to famous
            FileWriter fW = new FileWriter("src/FamousBallet/famous");
            PrintWriter out = new PrintWriter(fW);
            
            //Input data
            out.print(output.toString());
            
            //close the FileWriter and the PrintWriter
            fW.close();
            out.close();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Saving file failed.","ERROR",
                    JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Saving file failed.","ERROR",
                    JOptionPane.ERROR_MESSAGE);
        }
        
        //Clear the famous arraylist
        famous.clear();
        
        //Read the file again and reload the famous arraylist 
        readFromFile(FILE_NAME);
        
        //clear the JList
        famousJList.removeAll();
        
        //Display all the Ballets in the file.
        displayBallets();
        
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method readFromFile()
     * Read the database file and put all the ballets in the file in 
     * the famous arraylist.
     * @param filePath, where the database file is located
     * @see java.util.ArrayList
     * @author Yerong
     * Date: 11/29/2016
     */        
    public void readFromFile(String filePath){
        //Clear the arraylist first
        famous.clear();
        
        //Avoid file exceptions
        try{
            //Create a new file object
            File file = new File(filePath);
            
            //Read the file(database)
            Scanner read = new Scanner(file).useDelimiter("~~");
            
            //Read the first element that indicates the number of Ballets.
            int count=Integer.parseInt(read.next());
            
            //Load datas
            while(read.hasNext()){
                //Create new Ballet object
                Ballet famousBallet = new Ballet(read.next(),read.next(),
                        Integer.parseInt(read.next()),read.next(),read.next()
                        ,read.next());
                //Validate the new Ballet object
                if(Validation.isValidBallet(famousBallet))
                    famous.add(famousBallet);
                else
                    JOptionPane.showMessageDialog(null, "The Ballet " +
                            famousBallet.getName() + " is not valid!",
                            "ERROR",JOptionPane.ERROR_MESSAGE);
            }

            }catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "File not found!!","ERROR",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Invalid input file!","ERROR",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method displayBallets()
     * Check which sorting method is used and display all the ballets 
     * in order(sort by name/sort by year).
     * @see java.util.ArrayList
     * @author Yerong
     * Date: 11/29/2016
     */       
    public void displayBallets(){
        int index = famousJList.getSelectedIndex();
        
        
        //create a string array for names only
        String[] listData = new String[famous.size()];
        
        if(sortNameJCheckBoxMenuItem.isSelected()){
            //Sort the arraylist by names of Ballets
            insertionSortByName(famous);
            //Fill the String array of names only
            for(int i=0;i<famous.size();i++){
                listData[i]=famous.get(i).getName();
            }
        }
        else{
            //sort the arraylist by year of Ballets
            selectionSortByYear(famous);
            //Fill the String array of years and names
            for(int i=0;i<famous.size();i++){
                listData[i]=famous.get(i).getName()+", "+famous.get(i).getYear();
            }            
        }
        
        //Set the famousJList
        famousJList.setListData(listData);
        
        //If none is selected, select the first one.
        if(index<0) famousJList.setSelectedIndex(0);
        else famousJList.setSelectedIndex(index);
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method insertionSortByName()
     * Sort the famous arraylist by names using the insertion sort.
     * @param list the famous array list
     * @see java.util.ArrayList
     * @author Yerong
     * Date: 11/29/2016
     */    
    public void insertionSortByName(ArrayList<Ballet> list){
        int i = 0, j = 0;
        for(i=0;i<list.size();i++){
            Ballet next = list.get(i);
            //if compare method returns -, the first is less than the second, vice versa.
            for(j=i-1;j>=0&&next.getName().compareToIgnoreCase(
                    list.get(j).getName())<0;j--){
                //If j>0 and famous(j) is greater than the next, swap famous(j+1
                //) and famous(j).
                famous.set(j+1, list.get(j));
            }
            famous.set(j+1, next);
        }
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method selectionSortByYear()
     * Sort the famous arraylist by years using the selection sort.
     * @param list the famous array list
     * @see java.util.ArrayList
     * @author Yerong
     * Date: 11/29/2016
     */        
    public void selectionSortByYear(ArrayList<Ballet> list){
        //The current position of minimum value.
        int pos_min=0;
        
        //temp object used to compare.
        Ballet temp = list.get(0);
        
        //Find the minium value and put it in the current index position.
        for(int i=0;i<list.size();i++){
            pos_min=i;
            //assign famous(i) to temp object
            temp=list.get(i);
            for(int j=i;j<list.size();j++){
                //If the val is less than all of the values, record the pos.
                if(list.get(j).getYear()<list.get(pos_min).getYear())
                    pos_min=j;
            }
            //sawp the Ballet at pos_min and position i.
            famous.set(i, list.get(pos_min));
            famous.set(pos_min, temp);
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sortButtonGroup = new javax.swing.ButtonGroup();
        outputJTextArea = new javax.swing.JTextArea();
        labelJPanel = new javax.swing.JPanel();
        titleJLabel = new javax.swing.JLabel();
        selectJPanel = new javax.swing.JPanel();
        listJScrollPane = new javax.swing.JScrollPane();
        famousJList = new javax.swing.JList<>();
        operationJPanel = new javax.swing.JPanel();
        addJButton = new javax.swing.JButton();
        editJButton = new javax.swing.JButton();
        deleteJButton = new javax.swing.JButton();
        exitJButton = new javax.swing.JButton();
        dataJPanel = new javax.swing.JPanel();
        nameJLabel = new javax.swing.JLabel();
        nameJTextField = new javax.swing.JTextField();
        composerJLabel = new javax.swing.JLabel();
        composerJTextField = new javax.swing.JTextField();
        yearJLabel = new javax.swing.JLabel();
        yearJTextField = new javax.swing.JTextField();
        cityJLabel = new javax.swing.JLabel();
        cityJTextField = new javax.swing.JTextField();
        synopsisJLabel = new javax.swing.JLabel();
        synScrollPane = new javax.swing.JScrollPane();
        synopsisJTextArea = new javax.swing.JTextArea();
        linkJLabel = new javax.swing.JLabel();
        linkJTextField = new javax.swing.JTextField();
        famousJMenuBar = new javax.swing.JMenuBar();
        fileJMenu = new javax.swing.JMenu();
        printJMenuItem = new javax.swing.JMenuItem();
        printBalletJMenuItem = new javax.swing.JMenuItem();
        fileJSeparator = new javax.swing.JPopupMenu.Separator();
        exitJMenuItem = new javax.swing.JMenuItem();
        sortJMenu = new javax.swing.JMenu();
        sortNameJCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        sortYearJCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        dataJMenu = new javax.swing.JMenu();
        addJMenuItem = new javax.swing.JMenuItem();
        editJMenuItem = new javax.swing.JMenuItem();
        deleteJMenuItem = new javax.swing.JMenuItem();
        dataJSeparator = new javax.swing.JPopupMenu.Separator();
        searchJMenuItem = new javax.swing.JMenuItem();
        searchByCityJMenuItem = new javax.swing.JMenuItem();
        searchByComposerJMenuItem = new javax.swing.JMenuItem();
        helpJMenu = new javax.swing.JMenu();
        aboutJMenuItem = new javax.swing.JMenuItem();

        outputJTextArea.setColumns(20);
        outputJTextArea.setLineWrap(true);
        outputJTextArea.setRows(6);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Famous Ballets");
        setMinimumSize(new java.awt.Dimension(620, 480));
        setResizable(false);

        labelJPanel.setBackground(new java.awt.Color(204, 204, 204));

        titleJLabel.setFont(new java.awt.Font("Times New Roman", 0, 36)); // NOI18N
        titleJLabel.setText("Famous Ballets");
        titleJLabel.setToolTipText("Presenting maybe all the ballets for you.");
        labelJPanel.add(titleJLabel);

        getContentPane().add(labelJPanel, java.awt.BorderLayout.NORTH);

        selectJPanel.setBackground(new java.awt.Color(204, 204, 204));
        selectJPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        famousJList.setBackground(new java.awt.Color(204, 204, 204));
        famousJList.setToolTipText("Select a Ballet");
        famousJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                famousJListValueChanged(evt);
            }
        });
        listJScrollPane.setViewportView(famousJList);

        selectJPanel.add(listJScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 220, 310));

        operationJPanel.setBackground(new java.awt.Color(204, 204, 204));
        operationJPanel.setMinimumSize(new java.awt.Dimension(336, 30));
        operationJPanel.setPreferredSize(new java.awt.Dimension(336, 30));
        operationJPanel.setLayout(new java.awt.GridLayout(1, 5, 20, 0));

        addJButton.setBackground(new java.awt.Color(153, 153, 255));
        addJButton.setMnemonic('A');
        addJButton.setText("Add");
        addJButton.setToolTipText("Add new Ballet");
        addJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJButtonActionPerformed(evt);
            }
        });
        operationJPanel.add(addJButton);

        editJButton.setBackground(new java.awt.Color(153, 153, 255));
        editJButton.setMnemonic('E');
        editJButton.setText("Edit");
        editJButton.setToolTipText("Edit the selected Ballet");
        editJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editJButtonActionPerformed(evt);
            }
        });
        operationJPanel.add(editJButton);

        deleteJButton.setBackground(new java.awt.Color(153, 153, 255));
        deleteJButton.setMnemonic('D');
        deleteJButton.setText("Delete");
        deleteJButton.setToolTipText("Delete the selected Ballet");
        deleteJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteJButtonActionPerformed(evt);
            }
        });
        operationJPanel.add(deleteJButton);

        exitJButton.setBackground(new java.awt.Color(153, 153, 255));
        exitJButton.setMnemonic('X');
        exitJButton.setText("Exit");
        exitJButton.setToolTipText("Exit the program");
        exitJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitJButtonActionPerformed(evt);
            }
        });
        operationJPanel.add(exitJButton);

        selectJPanel.add(operationJPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 400, 754, -1));

        dataJPanel.setBackground(new java.awt.Color(204, 204, 204));
        dataJPanel.setForeground(new java.awt.Color(51, 153, 255));
        dataJPanel.setMinimumSize(new java.awt.Dimension(480, 187));
        dataJPanel.setPreferredSize(new java.awt.Dimension(480, 661));
        dataJPanel.setLayout(null);

        nameJLabel.setBackground(new java.awt.Color(204, 204, 204));
        nameJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        nameJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        nameJLabel.setText("Name:");
        dataJPanel.add(nameJLabel);
        nameJLabel.setBounds(10, 20, 150, 30);

        nameJTextField.setEditable(false);
        nameJTextField.setBackground(new java.awt.Color(204, 204, 204));
        nameJTextField.setForeground(new java.awt.Color(51, 51, 51));
        nameJTextField.setToolTipText("Name of the ballet.");
        nameJTextField.setSelectionColor(new java.awt.Color(102, 102, 102));
        dataJPanel.add(nameJTextField);
        nameJTextField.setBounds(170, 20, 254, 30);

        composerJLabel.setBackground(new java.awt.Color(204, 204, 204));
        composerJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        composerJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        composerJLabel.setText("Composer:");
        composerJLabel.setMaximumSize(new java.awt.Dimension(36, 17));
        composerJLabel.setMinimumSize(new java.awt.Dimension(36, 17));
        composerJLabel.setPreferredSize(new java.awt.Dimension(150, 30));
        dataJPanel.add(composerJLabel);
        composerJLabel.setBounds(50, 70, 110, 20);

        composerJTextField.setEditable(false);
        composerJTextField.setBackground(new java.awt.Color(204, 204, 204));
        composerJTextField.setForeground(new java.awt.Color(51, 51, 51));
        composerJTextField.setToolTipText("The composer of the ballet.");
        composerJTextField.setSelectionColor(new java.awt.Color(102, 102, 102));
        dataJPanel.add(composerJTextField);
        composerJTextField.setBounds(170, 70, 254, 30);

        yearJLabel.setBackground(new java.awt.Color(204, 204, 204));
        yearJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        yearJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        yearJLabel.setText("Year first performed:");
        yearJLabel.setMaximumSize(new java.awt.Dimension(36, 17));
        yearJLabel.setMinimumSize(new java.awt.Dimension(36, 17));
        yearJLabel.setPreferredSize(new java.awt.Dimension(150, 30));
        dataJPanel.add(yearJLabel);
        yearJLabel.setBounds(40, 120, 120, 20);

        yearJTextField.setEditable(false);
        yearJTextField.setBackground(new java.awt.Color(204, 204, 204));
        yearJTextField.setForeground(new java.awt.Color(51, 51, 51));
        yearJTextField.setToolTipText("When the ballet first performed.");
        yearJTextField.setSelectionColor(new java.awt.Color(102, 102, 102));
        dataJPanel.add(yearJTextField);
        yearJTextField.setBounds(170, 110, 254, 30);

        cityJLabel.setBackground(new java.awt.Color(204, 204, 204));
        cityJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cityJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        cityJLabel.setText("City first performed:");
        cityJLabel.setMaximumSize(new java.awt.Dimension(36, 17));
        cityJLabel.setMinimumSize(new java.awt.Dimension(36, 17));
        cityJLabel.setPreferredSize(new java.awt.Dimension(150, 30));
        dataJPanel.add(cityJLabel);
        cityJLabel.setBounds(40, 170, 120, 20);

        cityJTextField.setEditable(false);
        cityJTextField.setBackground(new java.awt.Color(204, 204, 204));
        cityJTextField.setForeground(new java.awt.Color(51, 51, 51));
        cityJTextField.setToolTipText("Where the ballet first performed.");
        cityJTextField.setMinimumSize(new java.awt.Dimension(6, 10));
        cityJTextField.setSelectionColor(new java.awt.Color(102, 102, 102));
        dataJPanel.add(cityJTextField);
        cityJTextField.setBounds(170, 160, 254, 30);

        synopsisJLabel.setBackground(new java.awt.Color(204, 204, 204));
        synopsisJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        synopsisJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        synopsisJLabel.setText("Synopsis:");
        synopsisJLabel.setMaximumSize(new java.awt.Dimension(36, 17));
        synopsisJLabel.setMinimumSize(new java.awt.Dimension(36, 17));
        synopsisJLabel.setPreferredSize(new java.awt.Dimension(150, 30));
        dataJPanel.add(synopsisJLabel);
        synopsisJLabel.setBounds(40, 210, 120, 20);

        synopsisJTextArea.setColumns(10);
        synopsisJTextArea.setForeground(new java.awt.Color(51, 51, 51));
        synopsisJTextArea.setLineWrap(true);
        synopsisJTextArea.setRows(5);
        synopsisJTextArea.setToolTipText("The synopsis of this Ballet show. Double click to show detail.");
        synopsisJTextArea.setSelectionColor(new java.awt.Color(102, 102, 102));
        synopsisJTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                synopsisJTextAreaMouseClicked(evt);
            }
        });
        synScrollPane.setViewportView(synopsisJTextArea);

        dataJPanel.add(synScrollPane);
        synScrollPane.setBounds(170, 210, 254, 30);

        linkJLabel.setBackground(new java.awt.Color(204, 204, 204));
        linkJLabel.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        linkJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        linkJLabel.setText("Link to Youtube:");
        linkJLabel.setToolTipText("Click here to watch the video.");
        linkJLabel.setMaximumSize(new java.awt.Dimension(36, 17));
        linkJLabel.setMinimumSize(new java.awt.Dimension(36, 17));
        linkJLabel.setPreferredSize(new java.awt.Dimension(150, 30));
        linkJLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                linkJLabelMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                linkJLabelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                linkJLabelMouseReleased(evt);
            }
        });
        dataJPanel.add(linkJLabel);
        linkJLabel.setBounds(40, 260, 120, 20);

        linkJTextField.setForeground(new java.awt.Color(51, 51, 51));
        linkJTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        linkJTextField.setToolTipText("Click the Youtube icon to access the url.");
        linkJTextField.setSelectionColor(new java.awt.Color(102, 102, 102));
        linkJTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                linkJTextFieldMouseClicked(evt);
            }
        });
        dataJPanel.add(linkJTextField);
        linkJTextField.setBounds(170, 260, 254, 30);

        selectJPanel.add(dataJPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 0, 460, 330));

        getContentPane().add(selectJPanel, java.awt.BorderLayout.WEST);

        fileJMenu.setMnemonic('F');
        fileJMenu.setText("File");

        printJMenuItem.setMnemonic('P');
        printJMenuItem.setText("Print the GUI");
        printJMenuItem.setToolTipText("Print the form");
        printJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printJMenuItemActionPerformed(evt);
            }
        });
        fileJMenu.add(printJMenuItem);

        printBalletJMenuItem.setMnemonic('R');
        printBalletJMenuItem.setText("Print the Ballet");
        printBalletJMenuItem.setToolTipText("Print the selected Ballet");
        printBalletJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printBalletJMenuItemActionPerformed(evt);
            }
        });
        fileJMenu.add(printBalletJMenuItem);
        fileJMenu.add(fileJSeparator);

        exitJMenuItem.setMnemonic('X');
        exitJMenuItem.setText("Exit");
        exitJMenuItem.setToolTipText("Exit the program");
        exitJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitJMenuItemActionPerformed(evt);
            }
        });
        fileJMenu.add(exitJMenuItem);

        famousJMenuBar.add(fileJMenu);

        sortJMenu.setMnemonic('S');
        sortJMenu.setText("Sort");

        sortButtonGroup.add(sortNameJCheckBoxMenuItem);
        sortNameJCheckBoxMenuItem.setMnemonic('N');
        sortNameJCheckBoxMenuItem.setSelected(true);
        sortNameJCheckBoxMenuItem.setText("Sort By Names");
        sortNameJCheckBoxMenuItem.setToolTipText("Sort the list by names");
        sortNameJCheckBoxMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortNameJCheckBoxMenuItemActionPerformed(evt);
            }
        });
        sortJMenu.add(sortNameJCheckBoxMenuItem);

        sortButtonGroup.add(sortYearJCheckBoxMenuItem);
        sortYearJCheckBoxMenuItem.setMnemonic('Y');
        sortYearJCheckBoxMenuItem.setText("Sort By Years");
        sortYearJCheckBoxMenuItem.setToolTipText("Sort the list by years first performed");
        sortYearJCheckBoxMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortYearJCheckBoxMenuItemActionPerformed(evt);
            }
        });
        sortJMenu.add(sortYearJCheckBoxMenuItem);

        famousJMenuBar.add(sortJMenu);

        dataJMenu.setMnemonic('M');
        dataJMenu.setText("Data Management");

        addJMenuItem.setMnemonic('N');
        addJMenuItem.setText("Add new Ballet");
        addJMenuItem.setToolTipText("Add a new Ballet");
        addJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(addJMenuItem);

        editJMenuItem.setMnemonic('E');
        editJMenuItem.setText("Edit the Ballet");
        editJMenuItem.setToolTipText("Edit the selected Ballet");
        editJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(editJMenuItem);

        deleteJMenuItem.setMnemonic('D');
        deleteJMenuItem.setText("Delete the Ballet");
        deleteJMenuItem.setToolTipText("Delete the selected Ballet");
        deleteJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(deleteJMenuItem);
        dataJMenu.add(dataJSeparator);

        searchJMenuItem.setMnemonic('G');
        searchJMenuItem.setText("General Search");
        searchJMenuItem.setToolTipText("Search a Ballet by name or year");
        searchJMenuItem.setActionCommand("");
        searchJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(searchJMenuItem);

        searchByCityJMenuItem.setMnemonic('C');
        searchByCityJMenuItem.setText("Search by city");
        searchByCityJMenuItem.setToolTipText("Search a Ballet by city name");
        searchByCityJMenuItem.setActionCommand("");
        searchByCityJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchByCityJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(searchByCityJMenuItem);

        searchByComposerJMenuItem.setMnemonic('O');
        searchByComposerJMenuItem.setText("Search by composer");
        searchByComposerJMenuItem.setToolTipText("Search a Ballet by composer name");
        searchByComposerJMenuItem.setActionCommand("");
        searchByComposerJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchByComposerJMenuItemActionPerformed(evt);
            }
        });
        dataJMenu.add(searchByComposerJMenuItem);

        famousJMenuBar.add(dataJMenu);

        helpJMenu.setMnemonic('H');
        helpJMenu.setText("Help");

        aboutJMenuItem.setMnemonic('B');
        aboutJMenuItem.setText("About this");
        aboutJMenuItem.setToolTipText("About Famous Ballets");
        aboutJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutJMenuItemActionPerformed(evt);
            }
        });
        helpJMenu.add(aboutJMenuItem);

        famousJMenuBar.add(helpJMenu);

        setJMenuBar(famousJMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method exitJButtonActionPerformed()
     * Exit the program
     * @return nothing
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */           
    private void exitJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitJButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitJButtonActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method exitJMenuItemActionPerformed()
     * call the exitJButton.doClick();
     * @return nothing
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */      
    private void exitJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitJMenuItemActionPerformed
        exitJButton.doClick();
    }//GEN-LAST:event_exitJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method sortNameJCheckBoxMenuItemActionPerformed()
     * call the displayBallets() and check the sort by name check box.
     * @return sorted ballets
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */         
    private void sortNameJCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortNameJCheckBoxMenuItemActionPerformed
        displayBallets();
    }//GEN-LAST:event_sortNameJCheckBoxMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method sortYearJCheckBoxMenuItemActionPerformed()
     * call the displayBallets() and check the sort by name check box.
     * @return sorted ballets
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */     
    private void sortYearJCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortYearJCheckBoxMenuItemActionPerformed
        displayBallets();
    }//GEN-LAST:event_sortYearJCheckBoxMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method famousJListValueChanged()
     * get the selected index and show the information about the selec-
     * -ted ballet.
     * @return information about the selected ballet
     * @param evt the event
     * @see javax.swing.event.ListSelectionEvent
     * @author Yerong
     * Date: 11/29/2016
     */        
    private void famousJListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_famousJListValueChanged
        //get the selected index
        int index = famousJList.getSelectedIndex();
        
        //If the index is valid, show information about the selected ballet
        if(index<0) famousJList.setSelectedIndex(0); else{
        synopsisJTextArea.setText(famous.get(index).getSynopsis());
        //Set the caret position.
        synopsisJTextArea.setCaretPosition(0);
        nameJTextField.setText(famous.get(index).getName());
        composerJTextField.setText(famous.get(index).getComposer());
        cityJTextField.setText(famous.get(index).getCity());
        yearJTextField.setText(String.valueOf(famous.get(index).getYear()));
        linkJTextField.setText(famous.get(index).getLink());
        //Set the caret position.
        linkJTextField.setCaretPosition(0);}
    }//GEN-LAST:event_famousJListValueChanged

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method checkBallet()
     * Check whether there is the same on as the selected ballet.
     * @return if the ballet is the same as the one in the arraylist
     * @param temp the ballet object
     * @author Yerong
     * Date: 11/29/2016
     */      
    public boolean checkBallet(Ballet temp){
        //Iterate all the ballets and compare with overrided method.
        for(int i=0;i<famous.size();i++){
            if(temp.equals(famous.get(i))) return true;
        }
        return false;
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method editJButtonActionPerformed()
     * Pop up the edit JDialogForm and allow the user to edit the sele-
     * -cted ballet information. This method also checks whether the edited
     * one is the same as the one in the list by checkBallet(Ballet temp0 method
     * @return edited ballet
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */          
    private void editJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editJButtonActionPerformed
        //Create a new Edit form with edit as true.
        Edit newEdit = new Edit(this,true, true);
        
        //Load the ballet information
        newEdit.loadBallet(famous.get(famousJList.getSelectedIndex()));
        
        //Set the Edit form visable
        newEdit.setVisible(true);
        
        //If user exits the edit dialog, do nothing; If not, switch the Ballet. 
        if(!(newEdit.getBallet()==null)){
            //If there is the same one, show message.
            if(checkBallet(newEdit.getBallet())==true) 
                JOptionPane.showMessageDialog(null, 
                        "You have added a ballet already in the list ","Warning",
                                JOptionPane.ERROR_MESSAGE);
            else{
                //Switch the ballet and display it
                famous.set(famousJList.getSelectedIndex(), newEdit.getBallet());
                reWriteFile();
                }
        }
    }//GEN-LAST:event_editJButtonActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method aboutJMenuItemActionPerformed()
     * Pop up the about form with all the information about the program
     * @return the about form
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */          
    private void aboutJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutJMenuItemActionPerformed
        About newAbout = new About(this,true);
        newAbout.setVisible(true);
    }//GEN-LAST:event_aboutJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method synopsisJTextAreaMouseClicked()
     * Pop up the message box showing the full synopsis.
     * @return the about form
     * @param evt the event
     * @see java.awt.event.MouseEvent
     * @author Yerong
     * Date: 11/29/2016
     */        
    private void synopsisJTextAreaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_synopsisJTextAreaMouseClicked
        JOptionPane.showMessageDialog(null, synopsisJTextArea.getText(),nameJTextField.getText(),JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_synopsisJTextAreaMouseClicked
    
    private void linkJTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkJTextFieldMouseClicked

    }//GEN-LAST:event_linkJTextFieldMouseClicked

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method deleteJButtonActionPerformed()
     * delete the selected ballet on the list.
     * @return the new list
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */              
    private void deleteJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteJButtonActionPerformed
        //Get the selected index
        int index = famousJList.getSelectedIndex();
        
        if(index<0) JOptionPane.showMessageDialog(null, "Please select at "+
                "least one Ballet.","ERROR",JOptionPane.ERROR_MESSAGE);
        else{
        //Choice is used to store the choice
        int choice = JOptionPane.showConfirmDialog(null, 
                "Are you sure you want to delete this ballet?", "Confirmation", 
                JOptionPane.YES_NO_OPTION);
        
        //If the user selected yes, delete the Ballet
        if(choice==0){        
        famous.remove(index);
        reWriteFile();} 
        }
    }//GEN-LAST:event_deleteJButtonActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method editJMenuItemActionPerformed()
     * call the editJButton.doClick()
     * @return the edited new ballet
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */      
    private void editJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editJMenuItemActionPerformed
        editJButton.doClick();
    }//GEN-LAST:event_editJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method addJMenuItemActionPerformed()
     * call the addJButton.doClick()
     * @return the new ballet
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */          
    private void addJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJMenuItemActionPerformed
        addJButton.doClick();
    }//GEN-LAST:event_addJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method deleteJMenuItemActionPerformed()
     * call the deleteJButton.doClick()
     * @return the new ballet list
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */        
    private void deleteJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteJMenuItemActionPerformed
        deleteJButton.doClick();
    }//GEN-LAST:event_deleteJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method addJButtonActionPerformed()
     * Pop up the add ballet JDialogForm and allow the user to add a n-
     * -ew ballet, and also check whether the new one is the same as t-
     * -he one in the list.
     * @return the new ballet and new list
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */       
    private void addJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJButtonActionPerformed
        //Create a new Edit JDialogForm
        Edit addEdit = new Edit(this, true, false);
        
        //Set it visible to the user
        addEdit.setVisible(true);
        
        //If user exits the edit dialog, do nothing; If not, add the Ballet. 
        if(!(addEdit.getBallet()==null)){  
            //If there is an existed one, show message; If not, add the new one.
           if(checkBallet(addEdit.getBallet())==true)
               JOptionPane.showMessageDialog(null, 
                        "The new Ballet is the same as the one in the list, or"+
                                " the new one has the same year and same city "+
                                "as the one in the list.","Same Ballet!",
                                JOptionPane.ERROR_MESSAGE);
           else{
               //Add the new Ballet and rewrite the file.
                famous.add(addEdit.getBallet());
                reWriteFile();}
                   }
    }//GEN-LAST:event_addJButtonActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method searchJMenuItemActionPerformed()
     * Search the ballet and select it on the list.
     * @return the selected ballet
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */           
    private void searchJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchJMenuItemActionPerformed
        //Asking for the key to search
        String key = JOptionPane.showInputDialog("Enter the name or the yaer of"
                +" the Ballet.");

        if(key!=null) searchBallet(key);
    }//GEN-LAST:event_searchJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method printJMenuItemActionPerformed()
     * Print the form using PrintUtilities.
     * @return the printed form
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */     
    private void printJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printJMenuItemActionPerformed
        // Pritn the form as a GUI
        //Thanks to Marty Hall, http://www.apl.jhu.edu/~hall/java/
        PrintUtilities.printComponent(this);
    }//GEN-LAST:event_printJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method printBalletJMenuItemActionPerformed()
     * Print the Ballet by calling printJButton.doClick()
     * @return the printed form
     * @param evt the event
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */       
    private void printBalletJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printBalletJMenuItemActionPerformed
        printJButton.doClick();
    }//GEN-LAST:event_printBalletJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method searchByCityJMenuItemActionPerformed()
     * Search the ballet database by city name, and locate the index of
     * the ballet.
     * @param key the key to search the ballet
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */          
    private void searchByCityJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchByCityJMenuItemActionPerformed
        //Asking fot the key
        String key = JOptionPane.showInputDialog("Enter the city name of the " +
                "Ballet.");
        /*If the user cancels the dialog form or input an empty value, select 
        the first item.*/
        if(key!=null|!key.isEmpty()){
        try{
            int index = linearSearchString(key,"city");
            famousJList.setSelectedIndex(index);
            System.out.println(key);
        }catch(Exception e){
            famousJList.setSelectedIndex(0);
        }
        }
    }//GEN-LAST:event_searchByCityJMenuItemActionPerformed

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method searchByComposerJMenuItemActionPerformed()
     * Search the ballet database by composer name, and locate the 
     * index of the ballet.
     * @param key the key to search the ballet
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */       
    private void searchByComposerJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchByComposerJMenuItemActionPerformed
        //Asking fot the key
        String key = JOptionPane.showInputDialog("Enter the composer name of" +
                " the Ballet.");
        /*If the user cancels the dialog form or input an empty value, select 
        the first item.*/
        if(key!=null|!key.isEmpty()){
        try{
            int index = linearSearchString(key,"city");
            famousJList.setSelectedIndex(index);
            System.out.println(key);
        }catch(Exception e){
            famousJList.setSelectedIndex(0);
        }
        }
    }//GEN-LAST:event_searchByComposerJMenuItemActionPerformed
     
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method linkJLabelMouseClicked()
     * open the default browser and access the youtube link
     * @param evt the event
     * @see java.awt.event.MouseEvent
     * @see java.net.URI
     * @see java.util.logging
     * @author Yerong
     * Date: 11/29/2016
     */      
    private void linkJLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkJLabelMouseClicked
        try {
            //get the url
            String url = linkJTextField.getText();
            
            //open the default browser and access the url
            Desktop.getDesktop().browse(URI.create(url));
        } catch (IOException ex) {
            //default exception handling.
            Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_linkJLabelMouseClicked

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method linkJLabelMousePressed()
     * set the label background color to gray to make it like a button
     * @param evt the event
     * @see java.awt.event.MouseEvent
     * @author Yerong
     * Date: 11/29/2016
     */       
    private void linkJLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkJLabelMousePressed
        linkJLabel.setBackground(Color.GRAY);
    }//GEN-LAST:event_linkJLabelMousePressed

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method linkJLabelMouseReleased()
     * set the label background color to white when released
     * @param evt the event
     * @see java.awt.event.MouseEvent
     * @author Yerong
     * Date: 11/29/2016
     */       
    private void linkJLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkJLabelMouseReleased
        linkJLabel.setBackground(Color.WHITE);
    }//GEN-LAST:event_linkJLabelMouseReleased

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method searchBallet()
     * Check whether the key is digits or character sets and search it.
     * @param key the key to search the ballet
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */      
    public void searchBallet(String key){
        /*If the user cancels the dialog form or input an empty value, select 
        the first item.*/
        if(key!=null||!key.isEmpty()){
            //Determine search by year or by name
            if(key.matches("1[0-9]{3}")){
                //Linear search the Ballet
                int indexYear = linearSearchYear(Integer.valueOf(key));
                
                //If the return value is less than 0, show the message
                if(indexYear<0)
                    JOptionPane.showMessageDialog(null, "Can't find the Ballet."
                            ,"Ballet not found",JOptionPane.ERROR);
                else famousJList.setSelectedIndex(indexYear);
            }
            else{
            //If it's character sets， linear search by it's key and name.
            int indexName = linearSearchString(key,"name");
            
            famousJList.setSelectedIndex(indexName);
            }
        }
    }

     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method linearSearchYear()
     * Search if there is a matched ballet in the list by linear search
     * the year of the ballet
     * @return the position of the ballet
     * @param key the key to search the ballet
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */        
    public int linearSearchYear(int key){
        //Iterate the famous JList and search it
        for(int i=0;i<famous.size();i++){
            //If matches, return the key
            if(key==famous.get(i).getYear()){
                famousJList.setSelectedIndex(i);
                return i;
            }
        }
        //If not matched, return -1
        return -1;
    }
    
     /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method findMin()
     * Search if there is a nearly matched ballet in the list by compa-
     * -ring the key to every Ballet name.
     * @return the position of the ballet
     * @param key the key to search the ballet
     * @see java.awt.event.ActionEvent
     * @author Yerong
     * Date: 11/29/2016
     */     
    public int linearSearchString(String key, String content){
        int position = 0;
        switch(content){
            case "name":
                for(int i=0;i<famous.size();i++){
                    if(famous.get(i).getName().contains(key))
                        position = i;}
                    break;
            case "city":
                for(int i=0;i<famous.size();i++){
                    if(famous.get(i).getCity().contains(key))
                        position = i;}
                    break;                    
            case "composer":
                for(int i=0;i<famous.size();i++){
                    if(famous.get(i).getComposer().contains(key))
                        position = i;}
                    break;
        }
        
        return position;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Splash mySplash = new Splash(1500);
        mySplash.showSplash();
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutJMenuItem;
    private javax.swing.JButton addJButton;
    private javax.swing.JMenuItem addJMenuItem;
    private javax.swing.JLabel cityJLabel;
    private javax.swing.JTextField cityJTextField;
    private javax.swing.JLabel composerJLabel;
    private javax.swing.JTextField composerJTextField;
    private javax.swing.JMenu dataJMenu;
    private javax.swing.JPanel dataJPanel;
    private javax.swing.JPopupMenu.Separator dataJSeparator;
    private javax.swing.JButton deleteJButton;
    private javax.swing.JMenuItem deleteJMenuItem;
    private javax.swing.JButton editJButton;
    private javax.swing.JMenuItem editJMenuItem;
    private javax.swing.JButton exitJButton;
    private javax.swing.JMenuItem exitJMenuItem;
    private javax.swing.JList<String> famousJList;
    private javax.swing.JMenuBar famousJMenuBar;
    private javax.swing.JMenu fileJMenu;
    private javax.swing.JPopupMenu.Separator fileJSeparator;
    private javax.swing.JMenu helpJMenu;
    private javax.swing.JPanel labelJPanel;
    private javax.swing.JLabel linkJLabel;
    private javax.swing.JTextField linkJTextField;
    private javax.swing.JScrollPane listJScrollPane;
    private javax.swing.JLabel nameJLabel;
    private javax.swing.JTextField nameJTextField;
    private javax.swing.JPanel operationJPanel;
    private javax.swing.JTextArea outputJTextArea;
    private javax.swing.JMenuItem printBalletJMenuItem;
    private javax.swing.JMenuItem printJMenuItem;
    private javax.swing.JMenuItem searchByCityJMenuItem;
    private javax.swing.JMenuItem searchByComposerJMenuItem;
    private javax.swing.JMenuItem searchJMenuItem;
    private javax.swing.JPanel selectJPanel;
    private javax.swing.ButtonGroup sortButtonGroup;
    private javax.swing.JMenu sortJMenu;
    private javax.swing.JCheckBoxMenuItem sortNameJCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem sortYearJCheckBoxMenuItem;
    private javax.swing.JScrollPane synScrollPane;
    private javax.swing.JLabel synopsisJLabel;
    private javax.swing.JTextArea synopsisJTextArea;
    private javax.swing.JLabel titleJLabel;
    private javax.swing.JLabel yearJLabel;
    private javax.swing.JTextField yearJTextField;
    // End of variables declaration//GEN-END:variables
}
